package TrafficControlSystem.TrafficSimulator;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import TrafficControlSystem.Common.Car;
import TrafficControlSystem.Common.Direction;
import TrafficControlSystem.Common.Light;
import TrafficControlSystem.Common.LightsInfo.LightsState;

public class Crossing
{
	private Light horizontal;
	private Light vertical;
	private ArrayList<Integer> xArray;
	private ArrayList<Integer> yArray;
	private int verticalTraffic;
	private int horizontalTraffic;
	/**
	 * 
	 */
	public Crossing(int x1, int x2, int y1, int y2)
	{
		horizontal = Light.GREEEN;
		vertical = Light.GREEEN;
		xArray = new ArrayList<Integer>();
		yArray = new ArrayList<Integer>();
		xArray.add(x1);
		xArray.add(x2);
		yArray.add(y1);
		yArray.add(y2);
		verticalTraffic = horizontalTraffic = 0;
	}
	
	/**
	 * @return the verticalTraffic
	 */
	public int getVerticalTraffic()
	{
		return verticalTraffic;
	}

	/**
	 * @return the horizontalTraffic
	 */
	public int getHorizontalTraffic()
	{
		return horizontalTraffic;
	}

	//czy nastepujace wspolrzedne naleza do tego skrzyzowania
	public boolean thisCrossing(int x, int y)
	{
		if(xArray.contains(x) && yArray.contains(y))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public boolean thisCrossing(Point p)
	{
		if(xArray.contains(p.x) && yArray.contains(p.y))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public Light getLight(Direction direction)
	{
		if(direction == Direction.UP || direction == Direction.DOWN)
		{
			return vertical;
		}
		else
		{
			return horizontal;
		}
	}
	
	public ArrayList<Point> getLighsPositions(Direction direction)
	{
		int minX,minY;
		if(xArray.get(0)<xArray.get(1))
		{
			minX = xArray.get(0);
		}
		else
		{
			minX = xArray.get(1);
		}
		if(yArray.get(0)<yArray.get(1))
		{
			minY = yArray.get(0);
		}
		else
		{
			minY = yArray.get(1);
		}
		
		ArrayList<Point> wynikowa = new ArrayList<Point>();
		if(direction == Direction.UP || direction == Direction.DOWN)
		{
			wynikowa.add(new Point(minX-1,minY-1));
			wynikowa.add(new Point(minX+2,minY+2));
		}
		else
		{
			wynikowa.add(new Point(minX-1,minY+2));
			wynikowa.add(new Point(minX+2,minY-1));
		}
		return wynikowa;
	}
	
	public void changeLight()
	{
		Light tmp = horizontal;
		horizontal = vertical;
		vertical = tmp;
	}
	
	public void setLight(LightsState ls)
	{
		if(ls == LightsState.HORIZONTALLY_GREEN)
		{
			this.horizontal = Light.GREEEN;
			this.vertical = Light.RED;
		}
		else
		{
			this.horizontal = Light.RED;
			this.vertical = Light.GREEEN;			
		}
	}
	
	public int getCarCount(final Car table[][])
	{
		int count = 0;
		for(int x : xArray)
		{
			for(int y : yArray)
			{
				if(table[x][y]!= null)
					count++;
			}
		}
		return count;
	}

	public Direction chooseDirection(Direction direction)
	{
		//zapisanie statystyk ruchu
		if(direction == Direction.UP)
		{
			verticalTraffic++;
		}
		if(direction == Direction.DOWN)
		{
			verticalTraffic++;
		}
		if(direction == Direction.LEFT)
		{
			horizontalTraffic++;
		}
		if(direction == Direction.RIGHT)
		{
			horizontalTraffic++;
		}
		
		
		
		Direction newDirection = Direction.UP;
		Random r = new Random(System.currentTimeMillis());
		int i = r.nextInt()%4;
		if(i<0)i*=-1;
		if(i==0)
		{
			newDirection = Direction.UP;
		}
		if(i==1)
		{
			newDirection = Direction.DOWN;
		}
		if(i==2)
		{
			newDirection = Direction.LEFT;
		}
		if(i==3)
		{
			newDirection = Direction.RIGHT;
		}
		
		while(newDirection == opposite(direction))
		{
			int j = r.nextInt()%4;
			if(j<0)j*=-1;
			if(j==0)
			{
				newDirection = Direction.UP;
			}
			if(j==1)
			{
				newDirection = Direction.DOWN;
			}
			if(j==2)
			{
				newDirection = Direction.LEFT;
			}
			if(j==3)
			{
				newDirection = Direction.RIGHT;
			}
		}
		
			if(newDirection == Direction.UP)
			{
//				System.out.println("zmiana na UP");
			}
			if(newDirection == Direction.DOWN)
			{
//				System.out.println("zmiana na DOWN");
			}
			if(newDirection == Direction.LEFT)
			{
//				System.out.println("zmiana na LEFT");
			}
			if(newDirection == Direction.RIGHT)
			{
//				System.out.println("zmiana na RIGHT");
			}
		
		
		return newDirection;
	}

	private Direction opposite(Direction direction)
	{
		if(direction == Direction.UP)
		{
			return Direction.DOWN;
		}
		else if(direction == Direction.DOWN)
		{
			return Direction.UP;
		}
		else if(direction == Direction.LEFT)
		{
			return Direction.RIGHT;
		}
		else
		{
			return Direction.LEFT;
		}
	}

	public void tickTraffic(Direction direction)
	{
		// TODO Auto-generated method stub
		if(direction == Direction.UP || direction == Direction.DOWN)
		{
			verticalTraffic++;
		}
		else
		{
			horizontalTraffic++;
		}
	}
	
}
